package core.view
{
	import core.model.PSharedObject;
	
	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.utils.setTimeout;
	
	import org.ais.event.TEvent;
	import org.ais.system.Ais;
	import org.aisy.display.USprite;
	import org.aisy.listoy.Listoy;
	import org.aisy.listoy.ListoyEnum;
	import org.aisy.listoy.ListoyEvent;
	import org.aisy.scroller.Scroller;
	import org.aisy.skin.AisySkin;

	public class PlayListView extends USprite
	{
		protected var bgMc:MovieClip;
		protected var _listoy:Listoy;
		protected var _scroller:Scroller;
		
		public function PlayListView()
		{
			init();
		}
		
		protected function init():void
		{
			AisySkin.MOBILE = true;
			
			bgMc = new SWF_PLAYLIST_BG();
			addChild(bgMc);
			
			_listoy = new Listoy();
			_listoy.NAME = "PLAY_LISTOY";
			_listoy.setLayout(ListoyEnum.LAYOUT_VERTICAL);
			_listoy.setPadding(3);
//			_listoy.setRowColumn(11, 1);
			_listoy.setBackground(true);
//			_listoy.x = 25;
//			_listoy.y = 65;
//			addChild(_listoy);
			_listoy.dynamic = {"mouseEnabled": false};
			
			_scroller = new Scroller();
			_scroller.setSource(_listoy);
			_scroller.setMoveFactor(3.3);
			_scroller.x = 25;
			_scroller.y = 65;
			addChild(_scroller);
			
			initializeList();
			
			setTimeout(__setTextFocus, 99);
			
			__addEvent();
			
			if (TEvent.trigger("UP_WINDOW_AIS", "SHOW", [this, "PLAY_LIST", 0, false, true, 0, 0, 1, false]) === false) {
				clear();
			}
		}
		
		protected function __addEvent():void
		{
			bgMc.btn_mc.addEventListener(MouseEvent.CLICK, __btnHandler);
			bgMc.txt_url.addEventListener(KeyboardEvent.KEY_UP, __txtKeyHandler);
			
//			_listoy.addEventListener(MouseEvent.MOUSE_WHEEL, __listoyHandler);
			
			TEvent.newTrigger(_listoy.NAME, __triggerHandler);
			TEvent.newTrigger("UP_WINDOW_NEW", __triggerHandler);
			TEvent.newTrigger("PLAY_LIST", __triggerHandler);
		}
		
		protected function __removeEvent():void
		{
			bgMc.btn_mc.removeEventListener(MouseEvent.CLICK, __btnHandler);
			bgMc.txt_url.removeEventListener(KeyboardEvent.KEY_UP, __txtKeyHandler);
			
			TEvent.removeTrigger("UP_WINDOW_NEW", __triggerHandler);
			TEvent.clearTrigger("PLAY_LIST");
		}
		
		protected function initializeList():void
		{
			_listoy.clearItem();
			_listoy.setRowColumn(PSharedObject.getInstance().data.p_list.length, 1);
			_listoy.setItemRenderer(PlayListItem);
			_listoy.setDataProvider(PSharedObject.getInstance().data.p_list);
			_listoy.initializeView();
			
			_scroller.setSize(_listoy.width, 239);
		}
		
		protected function __addURL():void
		{
			bgMc.txt_url.text = bgMc.txt_url.text.replace(/^\s+|\s+$/ig, "");
			if (!bgMc.txt_url.text) return;
			
			PSharedObject.getInstance().data.p_int = PSharedObject.getInstance().data.p_list.length;
			PSharedObject.getInstance().data.p_list.push([bgMc.txt_url.text, "Playlist_" + PSharedObject.getInstance().data.p_int]);
			PSharedObject.getInstance().flush();
			initializeList();
//			_listoy.setCurrentPosition(uint.MAX_VALUE);
			_scroller.layoutScroll(0, -_listoy.height);
			
			TEvent.trigger("IPLAYER_0", "PLAY", PSharedObject.getInstance().data.p_list[PSharedObject.getInstance().data.p_int][0]);
		}
		
		protected function __setTextFocus():void
		{
			if (null === bgMc) return;
			var len:uint = bgMc.txt_url.length;
			stage.focus = bgMc.txt_url;
			bgMc.txt_url.setSelection(len, len);
		}
		
		protected function __txtKeyHandler(e:KeyboardEvent):void
		{
			if (e.keyCode === 13) {
				__addURL();
			}
			else if (e.keyCode === 27) {
				bgMc.txt_url.text = "";
			}
		}
		
		protected function __btnHandler(e:MouseEvent):void
		{
			switch (e.target.name.replace(/btn\_/ig, "")) {
				case "close":
					TEvent.trigger("UP_WINDOW_NEW", "CLEAR", {"name": "PLAY_LIST"});
					break;
				case "play":
					__addURL();
					setTimeout(__setTextFocus, 99);
					break;
			}
			e = null;
		}
		
		protected function __listoyHandler(e:MouseEvent):void
		{
			var t:String;
			if (e.delta > 0) {
				t = ListoyEvent.PREVIOUS;
			}
			else {
				t = ListoyEvent.NEXT;
			}
			TEvent.trigger(_listoy.NAME, t);
			t = null;
		}
		
		protected function __triggerHandler(type:String, data:* = null):void
		{
			switch (type) {
				case "CLEAR":
					if (data.name === "PLAY_LIST") clear();
					break;
				case "CLEAR_ALL":
					clear();
					break;
				//PLAY_LIST
				case "CLEAR_LIST":
					TEvent.trigger("UP_WINDOW_NEW", "CLEAR", {"name": "PLAY_LIST"});
					break;
				//LISTOY NAME
				case "PLAY":
					if (PSharedObject.getInstance().data.p_int !== data) {
						PSharedObject.getInstance().data.p_int = data;
						PSharedObject.getInstance().flush();
						
						TEvent.trigger("IPLAYER_0", "PLAY", PSharedObject.getInstance().data.p_list[PSharedObject.getInstance().data.p_int][0]);
					}
					else {
						TEvent.trigger("IPLAYER_0", "TOGGLE_PAUSE");
					}
					break;
				case "DELETE":
					var _isPlay:Boolean;
					var _p:uint = PSharedObject.getInstance().data.p_int;
					if (data === _p) {
						_isPlay = true;
						var len:uint = PSharedObject.getInstance().data.p_list.length - 1;
						if (data === len) _p = 0;
						else _p++;
						_p = _p > len ? len : _p;
					}
					else if (_p > data) {
						_p--;
					}
					PSharedObject.getInstance().data.p_int = _p;
					PSharedObject.getInstance().data.p_list.splice(data, 1);
					PSharedObject.getInstance().flush();
//					var _curP:uint = _listoy.getCurrentPosition();
					var _y:Number = _listoy.y;
					initializeList();
//					if (_curP !== 0) {
//						_listoy.setDelay(0);
//						_listoy.setCurrentPosition(_curP);
//						_listoy.setDelay(0.8);
//					}
					if (_y !== 0) {
						_scroller.setDelay(0);
						_scroller.setMoveFactor(0);
						_scroller.layoutScroll(0, _y);
						_scroller.setDelay(0.3);
						_scroller.setMoveFactor(3.3);
					}
					if (_isPlay === true) {
						if (PSharedObject.getInstance().data.p_list[_p]) {
							TEvent.trigger("IPLAYER_0", "PLAY", PSharedObject.getInstance().data.p_list[_p][0]);
						}
					}
					break;
				case "CHANGE_PLAY_NAME":
					TEvent.trigger(Ais.IMain.Swf.get("UNETSTREAMY_IMOON", "swf").content.getNetStream().NAME, type, data);
					break;
			}
			type = null;
			data = null;
		}
		
		override public function get height():Number
		{
			return bgMc.height;
		}
		
		override public function clear():void
		{
			__removeEvent();
			bgMc = null;
			_listoy = null;
			_scroller = null;
			super.clear();
			Ais.IMain.stage.focus = Ais.IMain.stage;
		}
		
	}
}